<template>
  <div class="KnowledgeBaseDetail" style="height: 100%;">
    <div class="flex location-wrap">
      <span class="white">当前位置：</span>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/EmergencyPlan' }">应急预案</el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: '/KnowledgeBase' }">知识库</el-breadcrumb-item>
        <el-breadcrumb-item>详情</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="main">
      <div class="title">
        <p>{{ DetailList.name }}</p>
        <el-button plain size="small" @click="back" class="back">返回</el-button>
      </div>
      <LsdDialogTitle slot="dialog" :titleConfig="titleConfig" :activeNames="activeNames">
        <el-form slot="slot1" ref="form" :model="form" label-width="160px">
          <el-form-item label="文件名称：">
            {{ DetailList.name }}
          </el-form-item>
          <el-form-item label="文件号：">
            {{ DetailList.fileNum }}
          </el-form-item>
          <el-form-item label="封面：">
            <img :src="path" alt="" class="fengmian" id="fengmian" v-if="path">
          </el-form-item>
          <el-form-item label="描述：">
            {{ DetailList.description }}
          </el-form-item>
          <el-row>
            <el-col :span="12">
              <el-form-item label="发布(修订)时间：">
                {{ releaseTime }}
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="类别：">
                {{ DetailList.category }}
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="文件等级：">
            {{ DetailList.level }}
          </el-form-item>
          <el-form-item label="附件：">
            <router-link to="" class="" v-for="(item, index) in fileIdInfo" :key="index"><span @click="handlePreview">{{
              item.attachName }}</span></router-link>
          </el-form-item>
        </el-form>
        <el-form slot="slot2" ref="form" :model="form" label-width="160px" inline>
          <el-row>
            <el-col :span="8">
              <el-form-item label="创建人：">
                {{ DetailList.creator }}
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="单位及部门：">
                {{ DetailList.createDeptName }}
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="创建时间：">
                {{ DetailList.createTime }}
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </LsdDialogTitle>
    </div>
  </div>
</template>

<script>

import { getByIdApi, getStatisticsApi } from "@/api/EmengercyPlan/zsk";
import { loadFile } from "@/api/common.js";
export default {
  name: "KnowledgeBaseDetail",
  data() {
    return {
      pathId: "",
      titleConfig: [
        {
          title: "基本信息",
          name: "1",
          slot: "slot1"
        },
        {
          title: "创建信息",
          name: "2",
          slot: "slot2"
        }
      ],
      activeNames: ["1", "2"],
      form: {
      },
      DetailList: {},
      releaseTime: "",
      fileIdInfo: [],
      path: ""
    };
  },
  mounted() {
    this.pathId = JSON.parse(sessionStorage.getItem("id"));
    this.getById();
  },
  methods: {
    back() {
      this.$router.back();
    },
    getById() {
      console.log("route", this.pathId);
      getByIdApi({ id: this.pathId }).then(res => {
        if (res) {
          loadFile({ id: res.cover }).then(data => {
            this.path = URL.createObjectURL(data);
          });
          this.DetailList = res;
          this.releaseTime = (res.releaseTime).slice(0, 7);
          this.fileIdInfo = res.fileIdInfo;
          // this.path = res.cover;
          console.log("getById", res);
          console.log("path", this.path);
          console.log("cover", this.DetailList.cover);
        }
      });
    },
    // 下载
    async handlePreview() {
      console.log("this.fileIdInfo[0].attachId", this.fileIdInfo[0].attachId);
      const loading = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.6)"
      });
      loadFile({ id: this.fileIdInfo[0].attachId }).then(res => {
        const a = document.createElement("a");
        const objectUrl = URL.createObjectURL(res);
        a.setAttribute("href", objectUrl);
        a.setAttribute("download", this.fileIdInfo[0].attachName);
        a.click();
        loading.close();
      }).catch(() => {
        loading.close();
      });
      await getStatisticsApi({ id: this.pathId, type: 1 });
    }

  }
};
</script>

<style lang='less' scoped>
@import "../../../assets/style/common.less";
@import "../../../assets/style/ComponentStyle.less";
@import "../../../assets/style/pop-upNotification.less";

:deep(.el-form-item) {
  margin-bottom: 22px;

  .el-form-item__label {
    color: #303133;
  }
}

:deep(.el-breadcrumb__inner.is-link) {
  font-weight: 100;
  color: #fff;
}

:deep(.el-breadcrumb__item:last-child .el-breadcrumb__inner) {
  color: #3270ff;
}

:deep(.el-descriptions-item__label:not(.is-bordered-label)) {
  margin-right: 5px;
}

.main {
  height: 100%;
  border-radius: 4px;
  padding: 10px 60px;
  background-color: #fff;
  margin-top: 10px;
  box-sizing: border-box;
  overflow-y: auto;

  .title {
    position: relative;
    text-align: center;
    font-size: 28px;
    color: #303133;
    padding: 20px 0;

    .back {
      position: absolute;
      top: 80px;
      right: 20px;
      padding: 9px 18px;
    }
  }

  :deep(.el-descriptions) {
    font-size: 16px;
  }

  :deep(.desc-label) {
    min-width: 140px;
    justify-content: flex-end;
  }

  :deep(.el-form-item__label),
  :deep(.el-form-item__content) {
    font-size: 16px;
  }

  .link {
    text-decoration: underline;
    color: #3270ff;
    cursor: pointer;
  }

  .fengmian {
    width: 78px;
    height: 78px;
    object-fit: cover;
    vertical-align: middle;
  }
}
</style>
